// pages/newRegister/newRegister.js
var app = getApp();
var ea = require('../../utils/each.js');
var time = require('../../utils/util.js');
Page({
  
  /**
   * 页面的初始数据
   */
  data: {

    //多选
    items: [

    ],



    getxiaoquUrl: app.data.url + 'zhongxue/teacher/getxiaoqu',
    getxueduanUrl: app.data.url + 'zhongxue/teacher/getxueduan',
    getkemuUrl: app.data.url + 'zhongxue/teacher/getkemu',
    getzhiweiUrl: app.data.url + 'zhongxue/teacher/getzhiwei',
    getzhuceUrl: app.data.url + 'zhongxue/teacher/zuce',

    newRegisterUrl:[
        "/img/8a27a18bf612eadcb95729392a9ffe6.png"
    ],
 
    newUrl:[
      "/img/19.png"
    ],
    upUrl:[
      "/img/f0604ce67f88cedb8d7c78b22acc60f.png"
    ],
    //选择男女
    equlity: [
      { id: 0, names: "男", checked: 'true' },
      { id: 1, names: "女" },
    ],
    //学历
    xueli: [
      { id: 0, name: "初中" },
      { id: 1, name: "高中" },
      { id: 2, name: "大学" },
      { id: 3, name:"研究生"},
      { id: 4, name: "博士生" },
      { id: 5, name: "硕士生" },
      { id: 6, name: "其他" }
    ],
    index:"",
    //职位
    zhiwei:[],
    ymzhiwei:"",
    index1:"",
    //学段
    xueduan:[],
    ymxueduan:"",
    index2: "",
    //科目
    kemu:[],
    ymkemu:"",
    index3: "",
    //校区
    campus_id:[],
    ymcampus_id:"",
    index4: "",

    //生日
    nianlin:"",

    //上岗时间
    shangg:"",

    img:"",
    shuju: { sex: '男'}
  },
  newRegister:function(){
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  //获取input框所有内容
  inputWacth:function(e){
    // console.log(e);
    let item = e.currentTarget.dataset.model;
    // console.log(item);
    var shuju = this.data.shuju;
    shuju[item] = e.detail.value;
    console.log(shuju[item])
    this.setData({ shuju: shuju });
    this.setData({
      [item]: e.detail.value
    });

  },

  // 选择男女
  changebc: function (e) {
    // console.log(e);
    var index = e.currentTarget.dataset.index;
    // console.log(index)
    // console.log(this)
    var shujus = this.data.equlity;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(shujus, function (i, v) {
      if (i == index) {
        shuju.sex = v.names;
      }
    })
    this.setData({ shuju: shuju });
    // console.log(shuju);

  },

  //学历
  xueli: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var xueli = this.data.xueli;
    var shuju = this.data.shuju;
    ea.each(xueli, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.xueli = v.name;
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)
  },

  //职位
  zhiwei: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var zhiwei = this.data.zhiwei;
    var shuju = this.data.shuju;
    ea.each(zhiwei, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.zhiwei = v.id;
        var ymzhiwei = that.data.ymzhiwei
        ymzhiwei = v.name
        that.setData({ ymzhiwei: ymzhiwei})
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)
  },

  //学段
  xueduan: function (e) {
    // console.log(e);
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var xueduan = this.data.xueduan;
    var shuju = this.data.shuju;
    ea.each(xueduan, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.xueduan = v.id;
        var ymxueduan = that.data.ymxueduan
        ymxueduan = v.name;
        that.setData({ ymxueduan: ymxueduan})
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    console.log(shuju)  
    },

  //科目
  kemu: function (e) {
    var index = e.detail.value;
    // console.log(index)
    var that = this;
    var kemu = this.data.kemu;
    var shuju = this.data.shuju;
    ea.each(kemu, function (i, v) {
      if (i == index) {
        that.setData({ time: v });
        shuju.kemu = v.id;
        var ymkemu = that.data.kemu;
        ymkemu = v.name;
        that.setData({ymkemu:ymkemu})
        // console.log(shuju);
        that.setData({ shuju: shuju })
      }
    })
    // console.log(shuju)  
  },
  
  //校区
  checkboxChange: function (e) {
    var arr = e.detail.value;
    // console.log(campus_id)
    var that = this;
    // console.log(that);
    var shuju = this.data.shuju;
    var campus_id = arr.join(',');
    // console.log(campus_id)
    shuju.campus_id = campus_id;
    console.log(shuju)
  },

  //年龄
  shengri:function(e){
    // console.log(e);
    var that = this;
    var shengri = e.detail.value;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(shengri, function (i, v) {
      shuju.shengri = shengri;
        // console.log(shuju);
        that.setData({ shuju: shuju })
    })
    console.log(shuju)
  },

  //时间
  renzhi_time:function(e){
    // console.log(e);
    var that = this;
    var renzhi_time = e.detail.value;
    var shuju = this.data.shuju;
    // console.log(shuju);
    ea.each(renzhi_time, function (i, v) {
      shuju.renzhi_time = renzhi_time;
      // console.log(shuju);
      that.setData({ shuju: shuju })
    })
  },


  //上传文件
  shangchuan(e){
    var that = this
    console.log(that)
    var img = that.data.img
    console.log(img)
    if(img){
      if (imgs.length >= 1) {
        that.setData({
          lenMore: 1
        });
        setTimeout(function () {
          that.setData({
            lenMore: 0
          });
        }, 2500);
        return false;
      }
    }else{
      img = '';
    }
    var newimg = '';
    wx.chooseImage({
      count:1,  
      success: function(res) {
        
      },
    })
  },

  /**
   * 提交注册
   */
  zuce:function(){
    var that = this;
    var shuju = this.data.shuju;
    // console.log(shuju);
    var yanz = ea.yanzhen(shuju,{
      personal:'请输入个人简介',
      campus_id:'请选择你任职的校区',
      renzhi_time:'请选择你任职的时间',
      kemu:'请选择你任职的科目',
      xueduan:'请选择你任职的学段',
      zhiwei:'请选择你的职位',
      zhicheng:'请填写你的职称',
      xuexiao:'请填写你的毕业学校',
      xueli:'请选择你的最高学历',
      password:'请输入你的登录密码',
      email:'请填写你的邮箱',
      phone:'请填写你的联系电话',
      address:'请填写你的家庭住址',
      sex:'请选择你的性别',
      shengri:'请选择你的生日',
      name:'请填写你的姓名',
    })
    if (yanz) {
      wx.showToast({
        title: yanz,
        icon: 'none'
      })
      return;
    }
    var user = wx.getStorageSync('user')
    console.log(user);
    shuju.token = user.token;
    shuju.user_id = user.id;
    // console.log(shuju);
    // return
    wx.request({
      url: that.data.getzhuceUrl,
      method: 'post',
      data: shuju,
      success: function (res) {
        var data = res.data;
        // console.log(data)
        // return
        if (data.code == 200) {
          wx.showToast({
            title: '注册成功,请联系后台管理员审核',
            icon: 'none',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000)
        } else {
          wx.showToast({
            title: '注册失败',
            icon: 'none',
          })
        }
      }
    })


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var user = wx.getStorageSync('user')
    var campus_id = that.data.campus_id;
    var xueduan = that.data.xueduan;
    var kemu = that.data.kemu;
    // console.log(xiaoqu)
    wx.request({
      url: that.data.getxiaoquUrl,
      method:'post',
      success:function(res){
        var data = res.data;
        console.log(data)
        if(data.code = 200){
          var items = res.data.data;
          that.setData({ items: items})
        }
        console.log(items);
      }
    })
    wx.request({
      url: that.data.getxueduanUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var xueduan = res.data.data;
          that.setData({ xueduan: xueduan })
        }
      }
    })
    wx.request({
      url: that.data.getkemuUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var kemu = res.data.data;
          that.setData({ kemu: kemu })
        }
      }
    })
    wx.request({
      url: that.data.getzhiweiUrl,
      method: 'post',
      success: function (res) {
        var data = res.data;
        // console.log(data)
        if (data.code = 200) {
          var zhiwei = res.data.data;
          that.setData({ zhiwei: zhiwei })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})